package com.billionsfinance.hessian.core;


import com.billionsfinance.hessian.common.zookeeper.server.ServerSideZkThread;
import com.billionsfinance.hessian.model.ServerInfo;
import org.springframework.util.Assert;

import java.util.Set;

/**
 * @ ClassName: HessianServerInitFactory.java
 * @ Author: WeiHui-Z
 * @ Date: 2016/11/23 14:24
 * @ Version: v1.0.0
 */
public class HessianServerInitFactory extends HessianInitFactory {

	private ServerSideZkThread zkThread;

	/**
	 * 访问路径
	 */
	private String accessUrl;

	public void setAccessUrl(String accessUrl) {
		this.accessUrl = accessUrl;
	}

	@Override
	protected void starterZookeeper() {
		Assert.notNull(accessUrl, "accessUrl can not be null");

		Set<ServerInfo> serverInfoSet = scanPackages(accessUrl);
		if (serverInfoSet != null && !serverInfoSet.isEmpty()) {
			zkThread = new ServerSideZkThread(zkConfig, serverInfoSet);
			Thread thread = new Thread(zkThread);
			thread.setName("zookeeper注册");
			thread.start();
		}
	}

	@Override
	public void destroy() throws Exception {
		if (zkThread != null) {
			zkThread.close();
		}
	}
}